// alarm.js
import * as echarts from '../../components/ec-canvas/echarts';
const app = getApp();
var option = null;


function initChart(canvas, width, height, dpr) {
  option = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio:dpr
  });
  canvas.setChart(option);
  return option;
}


Page({

  data: {
    ec:{
      onInit:initChart
    },
    historyData:[]
  },

  //获取报警列表
  getDeviceAlarm(){
    let that=this;
    wx.request({
      url: app.globalData.url+'getDeviceAlarm/'+app.globalData.deviceCode,
      // url: app.globalData.url+'getDeviceAlarm/579851534952',
      data:{},
      method:'GET',
      header:{
        token:app.globalData.token
      },
      success:function(res){
        console.log("历史报警信息=====",res)
        let resData = res.data.data;
        let arr = [];
        let obj = that.countTimes(resData);

        for(var key in obj){
          arr.push(obj[key])
        }

        console.log("====",arr)
        that.setData({
          alarmArr:res.data.data
        })
        setTimeout(()=>{
          that.getCountNum(arr)
        },1000)
      }
    })
  },
  countTimes(data){
    var obj = {};
    return data.reduce(function(time, name) {
        if (name.alarmContent in time) {
            time[name.alarmContent]++;
        } else {
            time[name.alarmContent] = 1;
        }
        return time;
    }, {});

  },
  getCountNum(resData){
    option.setOption({

      title: {
        subtext: '(次数)',
        subtextStyle: {
          color: '#fff', 
        }
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
          type: 'none'        // 默认为直线，可选为：'line' | 'shadow'
        }
      },
      xAxis: {
        type: 'category',
        data: ['呼吸过快', '呼吸过慢', '心跳过快', '心跳过慢', '无心跳', '离床'],
        axisLabel: {
           show: true,
          textStyle: {
            color: '#fff',  //更改坐标轴文字颜色
            fontSize :10      //更改坐标轴文字大小
          }
         },
        axisLine:{
          lineStyle:{
           color:'#ededed' //更改坐标轴颜色
          }
        },
        axisTick: {
          show: true
        },
        splitLine: {
          show: false
        }
      },
      yAxis: {
        type: 'value',
        min:0,
        max:20,
        interval:5,
        axisLabel: {
          show: true,
          textStyle: {
            color:'#fff',
            fontSize: 14,
          },
          formatter: function(value){
            if(value == 0 || value == 20){
              return ' '
            } else {
              return value
            }
          }
        },
        axisLine:{
          lineStyle:{
           color:'#ededed' //更改坐标轴颜色
          }
        },
        axisTick: {
          show: false
        },
        splitLine: {
          show: false
        }
      },
       series: [{
        name:'报警',
        data: resData,
        type: 'bar',
        barWidth: '30',
        color: '#96fbd4'
      }]

    })
  },
  onLoad: function (options) {
    this.getDeviceAlarm()
  },

 
  onReady: function () {
    
  },

 
  onShow: function () {
    
  },
  


  onHide: function () {
    
  },


  onUnload: function () {
    
  },


  onPullDownRefresh: function () {
    
  },

  onReachBottom: function () {
    
  },

  onShareAppMessage: function () {
    
  }
})
